﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLib;
using Portal.Logic;
using Portal.Entity;
using Portal.Interface;
using System.Text;
using System.Data;

namespace Portal
{
    public partial class SecurityList : System.Web.UI.Page
    {
        SecurityLogic _logic = new SecurityLogic();
        ResourceLogic _res = new ResourceLogic();
        public string SecurityList_ConfirmDelete = Portal.Resources.lang.SecurityList_ConfirmDelete;
        protected void Page_Load(object sender, EventArgs e)
        {
            string Mnaem = Request.QueryString["Name"];
            if (!string.IsNullOrEmpty(Mnaem))
            {
                InitMenu(Mnaem);
            }
            else
            {
                if (!IsPostBack)
                {
                    BindGrid();

                    AspNetPager1.FirstPageText = Portal.Resources.lang.FirstPage;
                    AspNetPager1.PrevPageText = Portal.Resources.lang.PrevPage;
                    AspNetPager1.NextPageText = Portal.Resources.lang.NextPage;
                    AspNetPager1.LastPageText = Portal.Resources.lang.LastPage;
                    btnAdd.Text = Portal.Resources.lang.SecurityList_Add;
                }
            }
        }

        List<ResourceEntity> GetList()
        {
            List<ResourceEntity> list = new List<ResourceEntity>();
            object obj = ViewState["list"];
            if (obj == null)
            {
                obj = _res.GetResourceList("Menu");
                ViewState["list"] = obj;
            }
            return (List<ResourceEntity>)ViewState["list"];
        }

        List<ResourceEntity> GetList(List<ResourceEntity> list, int skip, int max)
        {
            List<ResourceEntity> temp = new List<ResourceEntity>();
            for (int i = skip; i < skip + max; i++)
            {
                if (list.Count > i)
                {
                    temp.Add(list[i]);
                }
            }
            return temp;
        }


        void BindGrid()
        {
            int skipResults = (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize;
            int maxResults = AspNetPager1.PageSize;
            AspNetPager1.RecordCount = GetList().Count;
            rptList.DataSource = GetList(GetList(), skipResults, maxResults);
            rptList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                string str = e.CommandArgument.ToString();
                _res.DeleteResource(ConvertUtil.ToInt32(str));
                ViewState["list"] = null;
                BindGrid();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('"+Portal.Resources.lang.SecurityList_DeleteSuccess+"!');", true);
            }
        }

        //查询菜单名称
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string MenuName = txtMenuName.Text.Trim();
            if(string.IsNullOrEmpty(MenuName)){
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('请填写查询条件!');", true);
                return;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from com_resource a where a.cnname like '%{0}%'",MenuName);
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
            rptList.DataSource = dt;
            rptList.DataBind();
        }

        //页面加载查询菜单
        void InitMenu(string Name) { 
        
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from com_resource a where a.cnname like '%{0}%'", Name);
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strSql.ToString());
            rptList.DataSource = dt;
            rptList.DataBind();
        }
    }
}